 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package abs.beans;


import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import abs.dao.CPerm100;
import abs.dao.CUse100;

//import logeo.CPerm100;

/**
 *
 * @author SISTEMAS
 */
@ManagedBean
@ApplicationScoped

public class MenuBean implements Serializable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//private CPerm100 perm100=new CPerm100();

    String permiso = null;
    private LoginBean login;
    String codcompania;
    String username;
    private String rol;
    CUse100 lookupUse100Facade=new CUse100();
    CPerm100 lookupPerm100Facade=new CPerm100();

    /** Creates a new instance of MenuBean */
    public MenuBean() {
        FacesContext context = FacesContext.getCurrentInstance();
        login = context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        codcompania = login.getCodcompania();
        username = login.getUsername();

    }

    public String getRol() {
        return rol;
    }

    public void setRol() throws SQLException {
    	
        this.rol = lookupUse100Facade.rolUsuario(username, codcompania);
    }

   

    

    /*public void validarAcceso(String programa) throws SQLException, IOException {
    
    if (perm100.buscarProgramaUsuario(programa)) {
    System.out.println("acceso concedido");
    
    } else {
    System.out.println("acceso rechazado");
    
    }
    
    }*/
    public void processAction(String codigo) throws SQLException {
        setRol();
        System.out.println(codigo);
        System.out.println(login.getUsername());
        System.out.println(login.getCodcompania());
        System.out.println(rol);
        


        if (lookupPerm100Facade.buscarProgramaUsuario(login.getCodcompania(), codigo,rol)) {
            System.out.println("acceso concedido");
            //FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Acceso concedido", "");
            //FacesContext.getCurrentInstance().addMessage(null, msg);
            RequestContext context = RequestContext.getCurrentInstance();
            context.addCallbackParam("descargar", true);

        } else {
            //System.out.println("acceso rechazado");
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Acceso Rechazado", "Usted no tiene acceso a este mòdulo");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            RequestContext context = RequestContext.getCurrentInstance();
            context.addCallbackParam("descargar", false);

        }
    }

    public void download() throws IOException {
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();

        //ec.responseReset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
        //ec.setResponseContentType(contentType); // Check http://www.w3schools.com/media/media_mimeref.asp for all types. Use if necessary ExternalContext#getMimeType() for auto-detection based on filename.
        //ec.setResponseContentLength(contentLength); // Set it with the file size. This header is optional. It will work if it's omitted, but the download progress will be unknown.
        ec.setResponseHeader("Content-Disposition", "attachment; filename=ejecutables/webIngresoRequisiciones.jar"); // The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.

        //OutputStream output = ec.getResponseOutputStream();
        // Now you can write the InputStream of the file to the above OutputStream the usual way.
        // ...

        fc.responseComplete(); // Important! Otherwise JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
    }
    
}
